using System;
using System.Collections.Generic;
using System.Text;
using System.Net;

namespace Updater
{
    class Program
    {
        static void Main(string[] args)
        {
            string mgsiUrl = @"https://bruce.isis.cclrc.ac.uk:18443/mgsi/rpc_soap.cgi";
            string fileServerUrl = @"https://bruce.isis.cclrc.ac.uk:28443/mgsi/filesvr.cgi";
            Console.WriteLine("Creating Mgsi object this may take a while");
            Mgsi.UdMgsi ud = new Mgsi.UdMgsi();
            System.Net.ServicePointManager.CertificatePolicy = new MyPolicy();
            ud.Url = mgsiUrl;
            string auth = "";
            Console.WriteLine("Mgsi object created");
			string choice;
			
			auth = loginToGrid(auth, ud);
			if (auth == null)
			{
				return;
			}

			do
			{
                Console.WriteLine("Please select an option:");
                Console.WriteLine();
				Console.WriteLine("\t-anno = Edit the annotation of the job");
                Console.WriteLine("\t-state = Edit the state id of the job");
                Console.WriteLine("\t-des = Edit the description of the job");
				Console.WriteLine("\t-quit = Quit the program");
				string input = Console.ReadLine();
                Console.WriteLine();
				input = input.ToLower();
				choice = input;

				#region Annotation
				if (input == "-anno")
				{
					Mgsi.Job j = new Mgsi.Job();
					Console.Write("Please enter a job id: ");
					string jid = Console.ReadLine();

					j = ud.getJobById(auth, int.Parse(jid));

					if (j.annotation != "")
					{
						Console.WriteLine("Current annotation = " + j.annotation);
						Console.Write("There is already an annotation, would you still like to change it? (Y/N): ");
						string ans = Console.ReadLine();
                        Console.WriteLine();
						ans = ans.ToLower();

						if (ans == "y")
						{
							Console.Write("Please enter an annotation: ");
							string anno = Console.ReadLine();
							j.annotation = anno;
							ud.updateJob(auth, j);
							Console.WriteLine();
							Console.Write("New Annotation is: " + j.annotation);
							Console.WriteLine();
						}
						else if (ans == "n")
						{
							Console.WriteLine("Not changing annotation - annotation = " + j.annotation);
							Console.WriteLine();
						}
						else
						{
							Console.WriteLine("Please enter either Y or N");
							Console.WriteLine();
						}
					}
					else
					{
						Console.WriteLine("Annotation = " + j.annotation);
						Console.Write("Please enter an annotation: ");
						string anno = Console.ReadLine();
						j.annotation = anno;
						ud.updateJob(auth, j);
						Console.WriteLine("New Annotation is: " + j.annotation);
						Console.WriteLine();
					}
				}
				#endregion

                #region State ID
                if (input == "-state")
                {
                    Mgsi.Job j = new Mgsi.Job();
                    Console.Write("Please enter a job id: ");
                    string jid = Console.ReadLine();

                    j = ud.getJobById(auth, int.Parse(jid));

                    Console.WriteLine();
                    Console.WriteLine("---------------------------");
                    Console.WriteLine("ID:\n\t1 = Incomplete\n\t2 = Suspended\n\t3 = Completed\n\t4 = Failed");
                    Console.WriteLine("---------------------------");
                    Console.WriteLine();
                    Console.WriteLine("Current state of job is: " + j.state_id);
                    Console.WriteLine();

                    Console.Write("Would you like to change the state ID? (Y/N): ");
                    string stateinput = Console.ReadLine();
                    Console.WriteLine();
                    if(stateinput.ToLower() == "y")
                    {
                        Console.Write("Please enter the new state id: ");
                        string stateS = Console.ReadLine();
                        Console.WriteLine();
                        int state = int.Parse(stateS);
                        if(state > 4 || state < 1)
                        {
                            Console.WriteLine("Error: you have entered an invalid number");
                        }
                        else
                        {
                            j.state_id = state;
                            ud.updateJob(auth, j);
                            Console.WriteLine("New state id is: " + j.state_id);
                        }
                    }
                    else if (stateinput.ToLower() == "n")
                    {
                        Console.WriteLine("Not changing the state id: " + j.state_id);
                        Console.WriteLine();
                    }
                    else
                    {
                        Console.WriteLine("Please enter either Y or N");
                        Console.WriteLine();
                    }

                }
                #endregion

                #region Description
                if (input == "-des")
                {
                    Mgsi.Job j = new Mgsi.Job();
                    Console.Write("Please enter a job id: ");
                    string jid = Console.ReadLine();

                    j = ud.getJobById(auth, int.Parse(jid));

                    Console.WriteLine("Current description of job is: " + j.description);

                    Console.Write("Would you like to change the description? (Y/N): ");
                    string desinput = Console.ReadLine();
                    Console.WriteLine();
                    if (desinput.ToLower() == "y")
                    {
                        Console.Write("Please enter the new description: ");
                        string des = Console.ReadLine();
                        Console.WriteLine();
                        j.description = des;
                        ud.updateJob(auth, j);
                        Console.WriteLine("Description has been updated to " + j.description);
                        Console.WriteLine();
                    }
                    else if (desinput.ToLower() == "n")
                    {
                        Console.Write("Not changing the description - description = " + j.description);
                        Console.WriteLine();
                    }
                    else
                    {
                        Console.Write("Please enter either Y or N");
                        Console.WriteLine();
                    }
                }
                #endregion


            } while (choice != "-quit");
        }

        private static string loginToGrid(string auth, Mgsi.UdMgsi ud)
        {
            try
            {
                auth = ud.login("crowlatt", "smtp0001");
                
                return auth;
            }
            catch (Exception)
            {
                Console.WriteLine("Error logging in. Please Try Again");
                return null;
                //Environment.Exit(-1);
            }
        }
    }

    class MyPolicy : ICertificatePolicy
    {
        public bool CheckValidationResult(ServicePoint srvPoint, System.Security.Cryptography.X509Certificates.X509Certificate certificate, WebRequest request, int problem)
        {
            return problem == 0
                || problem == -2146762487  // CertUNTRUSTEDROOT
                || problem == -2146762481; // CertCN_NO_MATCH
        }
    }
}
